package com.hurl.waguji.biz.download;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.hurl.library.spider.SimpleMultiUrlPattern;
import com.hurl.library.spider.SpiderException;
import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.DateUtils;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.DateService;
import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.JcsjHqHexunBO;
import com.hurl.waguji.persistence.JcsjHqHexunBOId;

class MrhqHexunProcessor extends BaseDownloadProcessor{

	public MrhqHexunProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
		context.setBoSaveMode(DownloadContext.BO_SAVE_MODE_INSERT);
	}

	private static final Logger log = Logger.getLogger(MrhqHexunProcessor.class);
	private Date lastTradingTime;
	private Date lastTradingDate;

	@Override
	public Date getRq() {
		return lastTradingDate;
	}

	@Override
	public void validate() throws Throwable {
		lastTradingTime = DateService.getLastTradingTimeFromWeb();
		lastTradingDate = DateUtils.getDate(this.lastTradingTime);

		/* 只能在交易日当天休市后才能下载 */
		validateNowAfterTradingTime(lastTradingTime);
		
		/* 不可重复下载 */
//		validateAlreadyDownloaded(lastTradingDate);
	}

	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {
		html = StringUtils.substringBetween(html, "dataArr = [","];");
		if(StringUtils.isBlank(html)){
			return ;
		}
		String prefix = null;
		String lx = null;
		if(url.contains("type=2&market=1")){
			prefix = "SH";
			lx = "SHA";
		}else if(url.contains("type=2&market=2")){
			prefix = "SZ";
			lx = "SZA";
		}else if(url.contains("type=3&market=1")){
			prefix = "SH";
			lx = "SHB";
		}else if(url.contains("type=3&market=2")){
			prefix = "SZ";
			lx = "SZB";
		}
			
		List<String> list = StringUtils.substringsBetween(html, "[", "]");
		JcsjHqHexunBO bo = null;
		String gpdm = null;
		for(String text : list){
			String[] itemarr = text.split(",");
			gpdm = prefix+StringUtils.substringBetween(itemarr[0],"'","'");
			
			if(context.getDao().queryBySQL(SqlReader.getSql("SQL12050301",new String[]{gpdm,DateUtils.formatDate(getRq(), "yyyy-MM-dd")})).setReadOnly(true).list().size()>0){
				log.debug(gpdm+"已经下载过，不用下载！");
				continue;
			}
			
			bo = new JcsjHqHexunBO();
			bo.setId(new JcsjHqHexunBOId(gpdm,getRq()));
			bo.setGpMc(StringUtils.substringBetween(itemarr[1],"'","'"));
			bo.setGpLx(lx);
			bo.setZxj(Double.parseDouble(itemarr[2]));
			bo.setZdf(Double.parseDouble(itemarr[3])/100);
			bo.setZs(Double.parseDouble(itemarr[4]));
			bo.setJk(Double.parseDouble(itemarr[5]));
			bo.setZg(Double.parseDouble(itemarr[6]));
			bo.setZd(Double.parseDouble(itemarr[7]));
			bo.setCjl(Double.parseDouble(itemarr[8]));
			bo.setCje(Double.parseDouble(itemarr[9]));
			bo.setHs(Double.parseDouble(itemarr[10])/100);
			bo.setZf(Double.parseDouble(itemarr[11])/100);
			bo.setLb(Double.parseDouble(itemarr[12])/100);
			bo.setLastupdate(new Date());
			collector.addBO(bo);
		}
	}

	@Override
	public void downloadFailure(String url, Throwable e) throws SpiderException {
		throw new SpiderException("下载错误，停止下载！",e);
		
	}

	@Override
	public void shutdown(boolean success, Throwable t) {
		if(!success){
//			context.getDao().queryByHQL("delete from "+JcsjHqHexunBO.class.getSimpleName()+" where id.rq=:rq").setDate("rq", lastTradingDate).executeUpdate();
		}
	}

	@Override
	public UrlPattern getUrlPattern() {
		SimpleMultiUrlPattern pattern = new SimpleMultiUrlPattern();
		pattern.addUrl("http://quote.tool.hexun.com/hqzx/quote.aspx?type=2&market=1&sorttype=0&updown=up&page=1&count=9999");
		pattern.addUrl("http://quote.tool.hexun.com/hqzx/quote.aspx?type=2&market=2&sorttype=0&updown=up&page=1&count=9999");
		pattern.addUrl("http://quote.tool.hexun.com/hqzx/quote.aspx?type=3&market=1&sorttype=0&updown=up&page=1&count=9999");
		pattern.addUrl("http://quote.tool.hexun.com/hqzx/quote.aspx?type=3&market=2&sorttype=0&updown=up&page=1&count=9999");
		return pattern;
	}
}
